package org.dtrd.modules.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.regex.Pattern;

/**
 * 旧数据文件处理
 * @author zcli
 */
@Slf4j
public class FileUtil {
	private static Pattern isNumber=  Pattern.compile("^-?\\d+(\\.\\d+)?$");
	/**
	 * 读取xls文件
	 * @param filePath
	 */
	public static void readExcelXlsFile(String filePath) {
		File file = new File(filePath);
		try {
			if (file.isFile() && file.exists()) {
				FileInputStream fis = new FileInputStream(file);
				Workbook wb = new HSSFWorkbook(fis);
				int sheetNum = wb.getNumberOfSheets();
				Sheet sheet = null;
				int totalRow = 0;
				for (int i = 0; i < sheetNum; i++) {
					log.info("------第{}个Sheet------", 1+i);
					sheet = wb.getSheetAt(i);
					int firstRowIndex = sheet.getFirstRowNum() + 1;
					int lastRowIndex = sheet.getLastRowNum();
					for (int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) {
						Row row = sheet.getRow(rIndex);
						if (row != null) {
							String pName = getCellNameValue(row.getCell(0));
							Date date = getCellDateValue(row.getCell(1));

							log.info("=rIndex:{}, value:{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}", rIndex, pName, date, getCellValue(row.getCell(2)),
									getCellValue(row.getCell(3)), getCellValue(row.getCell(4)), getCellValue(row.getCell(5)),
									getCellValue(row.getCell(6)), getCellValue(row.getCell(7)), getCellValue(row.getCell(8)),
									getCellValue(row.getCell(9)), getCellValue(row.getCell(10)), getCellValue(row.getCell(11)),
									getCellValue(row.getCell(12)), getCellValue(row.getCell(13)), getCellValue(row.getCell(14)),
									getCellValue(row.getCell(15)), getCellValue(row.getCell(16)), getCellValue(row.getCell(17)));
							// 查询患者信息

							if (true) {
								// 添加血糖

								// 添加身高体重

								// 添加尿酮

								// 添加腰围

								// 添加血压和心率


							}
							totalRow++;
						}
					}
				}
				log.info("------------totalRow-----------{}", totalRow);
			}
		} catch(FileNotFoundException e1){
			log.error("文件不存在");
		} catch(IOException e){
			log.error("读取文件异常");
		}
	}

	// 添加血糖
	public static int addSugarValue() {

		return 0;
	}
	// 添加身高体重
	public static int addHeightWeight() {

		return 0;
	}
	// 添加尿酮
	public static int addKetone() {

		return 0;
	}
	// 添加腰围
	public static int addWaistline() {

		return 0;
	}
	// 添加血压和心率
	public static int addBloodPressure() {

		return 0;
	}



	// 获取日期
	public static Date getCellDateValue(Cell cell) {
		Date ret ;
		if (null != cell) {
			try {
				ret = cell.getDateCellValue();
			} catch (Exception e) {
				ret = null;
			}
		} else {
			ret = null;
		}
		return ret;
	}
	// 获取名字
	public static String getCellNameValue(Cell cell) {
		String ret ;
		if (null != cell) {
			try {
				ret = cell.getStringCellValue();
			} catch (Exception e) {
				ret = "";
			}
		} else {
			ret = "";
		}
		return ret;
	}

	// 获取其他测量值
	public static Object getCellValue(Cell cell) {
		Object ret = "";
		if (null != cell) {
			switch (cell.getCellType()) {
				case BLANK:
				case ERROR:
					ret = 0.0f;
					break;
				case STRING:
					ret = cell.getStringCellValue();
					if (!isNumber(String.valueOf(ret))) {
						ret = 0.0f;
					}
					break;
				case NUMERIC:
					ret = cell.getNumericCellValue();
					if (!isNumber(String.valueOf(ret))) {
						ret = 0.0f;
					}
					break;
				case BOOLEAN:
					ret = cell.getBooleanCellValue();
					break;
				case FORMULA:
					try {
						ret = cell.getDateCellValue();
					} catch (Exception e) {
						//log.error("****************{}", cell.getCellFormula());
						ret = cell.getCellFormula();
						if (!isNumber(String.valueOf(ret))) {
							ret = 0.0f;
						}
					}
					break;
				default:
					ret = cell.getStringCellValue();
					if (!isNumber(String.valueOf(ret))) {
						ret = 0.0f;
					}
					break;
			}
		} else {
			ret = "";
		}
		return ret;
	}


	/**
	 * format cell value
	 * @param string
	 * @return
	 */
	public static Float formatCell(String string) {
		if(StringUtils.isBlank(string)){
			return 0.0f;
		}
		if (!isNumber.matcher(string).matches()) {
			return 0.0f;
		}
		return Float.valueOf(string);
	}

	/**
	 * 验证是否非数字
	 */
	public static boolean isNumber(String string) {
		if(StringUtils.isBlank(string)){
			return false;
		}
		return isNumber.matcher(string).matches();
	}

	public static String getUrlFileName(String fileName) {
		// 去掉 https:// 前缀
		String url = fileName.substring(6);
		String[] split = url.split("/");
		return split[split.length-1];
	}


	/**
	 * main
	 * @param args
	 */
	public static void main(String[] args) {
//		String str = "D:\\work\\file\\out\\jialer\\逆糖\\患者日常血糖、血压记录.xls";
//		readExcelXlsFile(str);
		System.out.println(getUrlFileName("https://qr-store-image.oss-cn-guangzhou.aliyuncs.com/img/test3.png"));
	}

}
